package org.zfes.snowier.zbss.xzy.biz.vedio.service.impl;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import com.google.common.collect.Lists;
import org.zfes.snowier.core.data.SelectVo;
import org.zfes.snowier.zbss.xzy.biz.vedio.service.IDeviceVedioDirService;
import org.zfes.snowier.zbss.xzy.vedio.VedioDirDto;
import org.zfes.snowier.zbss.xzy.biz.device.model.Device;
import org.zfes.snowier.zbss.xzy.biz.device.service.IDeviceService;
import org.zfes.snowier.zbss.xzy.biz.vedio.dao.DeviceVedioDirMapper;
import org.zfes.snowier.zbss.xzy.biz.vedio.model.DeviceVedioDir;

import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class DeviceVedioDirServiceImpl implements IDeviceVedioDirService{
 	@Autowired
	private DeviceVedioDirMapper deviceVedioDirMapper;
	
 	
 	@Autowired
	private IDeviceService deviceService;
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createDeviceVedioDir(DeviceVedioDir deviceVedioDir) {
		ZBeanUtil.setCreateInfo(deviceVedioDir);
		ZBeanUtil.validateBean(deviceVedioDir);
		deviceVedioDirMapper.insertSelective(deviceVedioDir);
	}
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createBatch(VedioDirDto  vedioDirDto) {
		String deviceNum=vedioDirDto.getDeviceNum();
		ZAssert.hasText(deviceNum, "设备编号不能为空");
		ZAssert.notEmpty(vedioDirDto.getFileNames(), "文件目录不能为空");
		
		Device device = deviceService.loadByDeviceNum(deviceNum).orElseThrow(()->ZAlert.newSLE("未查询到设备"));
		
		Long deviceId = device.getId();
		
		deviceVedioDirMapper.batchInsert(
				vedioDirDto.getFileNames().stream()
				.map(fileName->new DeviceVedioDir( deviceId,  deviceNum,  fileName))
				.collect(Collectors.toList()));
	}
	
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<DeviceVedioDir> loadDeviceVedioDirById(Long id) {
		if(id!=null){
			return Optional.ofNullable(deviceVedioDirMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateDeviceVedioDir(DeviceVedioDir deviceVedioDir) {
		ZBeanUtil.setUpdatedInfo(deviceVedioDir);
		ZBeanUtil.validateBean(deviceVedioDir);
		deviceVedioDirMapper.updateByPrimaryKeySelective(deviceVedioDir);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteDeviceVedioDir(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		deviceVedioDirMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setDeviceVedioDirStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		deviceVedioDirMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadDeviceVedioDirDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(deviceVedioDirMapper.selectListCount(pm), deviceVedioDirMapper.selectMapListPage(pm));
		return ds;
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<SelectVo> loadDeviceVedioDirSelectVo(String keywords) {
		if(ZStrUtil.hasText(keywords)) {
			
		}
		List<SelectVo> voList=deviceVedioDirMapper.selectDropList(Byte.valueOf("1"));
		
		return voList==null?Lists.newArrayList():voList;
	}
    
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<DeviceVedioDir> loadVedioDirByDeviceId(Long deviceId){
		if(deviceId==null) {
			return Lists.newArrayList();
		}
		
		return deviceVedioDirMapper.selectByDeviceId(deviceId);
		
	}
}